﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;

public partial class Admin_AddSubCategory : System.Web.UI.Page
{
   
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            FillSubCategory();
        }
    }
    
    protected void btnSave_Click(object sender, EventArgs e)
    {
        if (txtJobsubType.Text == "")
        {
       
        }
        string jobcatid = Request.QueryString["jobcatid"];
        JobSiteStarterKit.DAL.DBAccess objInsert = new JobSiteStarterKit.DAL.DBAccess();
        System.Data.SqlClient.SqlParameterCollection objInsert1 = (System.Data.SqlClient.SqlParameterCollection)objInsert.Parameters;
        objInsert1.Add("@jobcatId", SqlDbType.NVarChar, 10).Value = jobcatid;
        objInsert1.Add("@subcatName", SqlDbType.NVarChar, 10).Value = txtJobsubType.Text.Trim();
        objInsert1.Add("@Remark", SqlDbType.NVarChar, 10).Value = txtsubtype.Text.Trim();
        objInsert.ExecuteNonQuery("Proc_InsertJob_SubCategory");
        lblmsg.Visible = true;
    }
    protected void BtnRefresh_Click(object sender, EventArgs e)
    {
        FillSubCategory();
    }

    public void FillSubCategory()
    {

        string jobcatid = Request.QueryString["jobcatid"];
        JobSiteStarterKit.DAL.DBAccess objselect = new JobSiteStarterKit.DAL.DBAccess();
        System.Data.SqlClient.SqlParameterCollection objselect1 = (System.Data.SqlClient.SqlParameterCollection)objselect.Parameters;
        objselect1.Add("@jobcatId", SqlDbType.Int).Value = jobcatid;
        DataSet DsetJobSubType = new DataSet();
        DsetJobSubType = objselect.ExecuteDataSet("Proc_GetJobSubCategory");
        DGrdSubCategory.DataSource = DsetJobSubType.Tables[0].DefaultView;
        DGrdSubCategory.DataBind();

    }
    protected void DGrdSubCategory_RowEditing(object sender, GridViewEditEventArgs e)
    {
        DGrdSubCategory.EditIndex = e.NewEditIndex;
        FillSubCategory();
    }
    protected void DGrdSubCategory_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
    {
        DGrdSubCategory.EditIndex = -1;
        FillSubCategory();
    }
    protected void DGrdSubCategory_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {
        string subcategory = "";
        string remarks = "";
        string subid = DGrdSubCategory.DataKeys[e.RowIndex].Value.ToString();
        JobSiteStarterKit.DAL.DBAccess objUpdate = new JobSiteStarterKit.DAL.DBAccess();
        System.Data.SqlClient.SqlParameterCollection objUpdate1 = (System.Data.SqlClient.SqlParameterCollection)objUpdate.Parameters;
        
        Control ctrcat = new Control();
        Control ctrremark = new Control();
        ctrcat = DGrdSubCategory.Rows[e.RowIndex].FindControl("txtcat");
        ctrremark = DGrdSubCategory.Rows[e.RowIndex].FindControl("txtRemark");
        if (ctrcat != null)
        {
            TextBox txtcategory = (TextBox)ctrcat;
            subcategory = txtcategory.Text;
        }
        if (ctrremark != null)
        {
            TextBox txtRemark = (TextBox)ctrremark;
            remarks = txtRemark.Text;
        }
        objUpdate1.Add("@subcatId", SqlDbType.NVarChar, 10).Value = subid;
       
        objUpdate1.Add("@SubcategoryName", SqlDbType.NVarChar, 500).Value = subcategory;
        objUpdate1.Add("@SubcategoryRemarks", SqlDbType.NVarChar, 1000).Value = remarks;
        objUpdate.ExecuteNonQuery("Proc_UpdateJobSubCategory");
        DGrdSubCategory.EditIndex = -1;
        FillSubCategory();
    }
    protected void DGrdSubCategory_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        string id = DGrdSubCategory.DataKeys[e.RowIndex].Value.ToString();
        JobSiteStarterKit.DAL.DBAccess objDelete = new JobSiteStarterKit.DAL.DBAccess();
        System.Data.SqlClient.SqlParameterCollection objDelete1 = (System.Data.SqlClient.SqlParameterCollection)objDelete.Parameters;
        objDelete1.Add("@subcatId", SqlDbType.Int).Value = id;
        objDelete.ExecuteNonQuery("Proc_DeleteJobSubCategory");
        FillSubCategory();
    }
}
